package com.ruoyi.project.system.controller;

import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.domain.UserNotice;
import com.ruoyi.project.system.service.IUserNoticeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 通知公告Controller
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/userNotice")
public class UserNoticeController extends BaseController
{
    @Autowired
    private IUserNoticeService UserNoticeService;

    @Autowired
    private TokenService tokenService;

    /**
     * 查询通知公告列表
     */
    //@PreAuthorize("@ss.hasPermi('system:usernotice:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserNotice UserNotice)
    {
        startPage();
        List<UserNotice> list = UserNoticeService.selectUserNoticeList(UserNotice);
        return getDataTable(list);
    }

    /**
     * 导出通知公告列表
     */
    @PreAuthorize("@ss.hasPermi('system:usernotice:export')")
    @Log(title = "通知公告", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(UserNotice UserNotice)
    {

        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            UserNotice.setCollegeId(sysUser.getDeptId());
        }
        List<UserNotice> list = UserNoticeService.selectUserNoticeList(UserNotice);
        ExcelUtil<UserNotice> util = new ExcelUtil<UserNotice>(UserNotice.class);
        return util.exportExcel(list, "UserNotice");
    }

    /**
     * 获取通知公告详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:usernotice:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(UserNoticeService.selectUserNoticeById(id));
    }

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:usernotice:add')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody UserNotice userNotice)
    {

        userNotice.setContent(URLDecoder.decode(userNotice.getContent()));
        return toAjax(UserNoticeService.insertUserNotice(userNotice));
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:usernotice:edit')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserNotice UserNotice)
    {
        UserNotice.setContent(URLDecoder.decode(UserNotice.getContent()));
        return toAjax(UserNoticeService.updateUserNotice(UserNotice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasPermi('system:usernotice:remove')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(UserNoticeService.deleteUserNoticeByIds(ids));
    }
}
